import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //查询
        //m1();


        //条件查询
        //m2();

        //增加
        //m3();
        //删除
        //m4();

        //修改
         m5();



    }

    private static void m4() {
        try {
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";

            try {
                Connection connection = DriverManager.getConnection(url, "root", "123456");
                //if(connection!=null)
                //   {
                //     System.out.println("连接成功");
                //    }
                //3、写sql
                String sql = "delete from user where id=4";

                //4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //5、有参数设置参数
                int resultSet = preparedStatement.executeUpdate();
                if(resultSet>0){
                    System.out.println("删除成功");
                }

                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    public static void m5() {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你要更改的id:");
            int id=sc.nextInt();
            System.out.println("请输入你要更改的姓名：");
            String name = sc.next();
            System.out.println("请输入你要更改的密码：");
            String pass = sc.next();
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";

            try {
                Connection connection = DriverManager.getConnection(url, "root", "123456");
                //if(connection!=null)
                //   {
                //     System.out.println("连接成功");
                //    }
                //3、写sql
                String sql = "update user set name=? ,pass=? where id=?";

                //4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //5、有参数设置参数
                preparedStatement.setObject(1, name);
                preparedStatement.setObject(2, pass);
                preparedStatement.setObject(3, id);
                int resultSet = preparedStatement.executeUpdate();
                if(resultSet>0){
                    System.out.println("修改成功");
                }

                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }


    private static void m3() {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的姓名：");
            String name = sc.next();
            System.out.println("请输入你的密码：");
            String pass = sc.next();
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";

            try {
                Connection connection = DriverManager.getConnection(url, "root", "123456");
                //if(connection!=null)
                //   {
                //     System.out.println("连接成功");
                //    }
                //3、写sql
                String sql = "insert into user values (3,?,?)";

                //4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //5、有参数设置参数
                preparedStatement.setObject(1, name);
                preparedStatement.setObject(2, pass);
                int resultSet = preparedStatement.executeUpdate();
                if(resultSet>0){
                    System.out.println("插入成功");
                }

                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    private static void m2() {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的姓名：");
            String name = sc.next();
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";

            try {
                Connection connection = DriverManager.getConnection(url, "root", "123456");
                //if(connection!=null)
                //   {
                //     System.out.println("连接成功");
                //    }
                //3、写sql
                String sql = "select * from user where name like ?";

                //4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //5、有参数设置参数
                preparedStatement.setObject(1,"%"+name+"%");
                //6、执行sql
                ResultSet resultSet = preparedStatement.executeQuery();
                //7、遍历结果集
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name1=resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println(id + "-----" + name1 + "-----" + pass);
                }
                resultSet.close();
                preparedStatement.close();
                connection.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }


    private static void m1() {
        try {
            //1、加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";

            try {
                Connection connection = DriverManager.getConnection(url, "root", "123456");
                //if(connection!=null)
                //   {
                //     System.out.println("连接成功");
                //    }
                //3、写sql
                String sql = "select * from user";

                //4、预编译sql
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                //5、有参数设置参数
                //6、执行sql
                ResultSet resultSet = preparedStatement.executeQuery();
                //7、遍历结果集
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println(id + "-----" + name + "-----" + pass);
                }
                resultSet.close();
                preparedStatement.close();
                connection.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

}